package com.boot.sample.shiro.config;

import com.boot.sample.shiro.dbo.Permission;
import com.boot.sample.shiro.dbo.Role;
import com.boot.sample.shiro.dbo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ShiroRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String userName = user.getUserName();

        System.out.println("用户" + userName + "获取权限-----ShiroRealm.doGetAuthorizationInfo");
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        // 获取用户角色集
        List<Role> roleList = findRoleByUserName(userName);
        Set<String> roleSet = new HashSet<String>();
        for (Role r : roleList) {
            roleSet.add(r.getName());
        }
        simpleAuthorizationInfo.setRoles(roleSet);

        // 获取用户权限集
        List<Permission> permissionList = findPermissionByUserName(userName);
        Set<String> permissionSet = new HashSet<String>();
        for (Permission p : permissionList) {
            permissionSet.add(p.getName());
        }
        simpleAuthorizationInfo.setStringPermissions(permissionSet);
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 获取用户输入的用户名和密码
        String userName = (String) authenticationToken.getPrincipal();
        String password = new String((char[]) authenticationToken.getCredentials());

        System.out.println("用户" + userName + "认证-----ShiroRealm.doGetAuthenticationInfo");

        // 通过用户名到数据库查询用户信息
        User user = findByUserName(userName);

        if (user == null) {
            throw new UnknownAccountException("用户名或密码错误！");
        }
        if (!password.equals(user.getPassword())) {
            throw new IncorrectCredentialsException("用户名或密码错误！");
        }
        if (user.getStatus().equals("0")) {
            throw new LockedAccountException("账号已被锁定,请联系管理员！");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
        return info;
    }


    private User findByUserName(String userName){
        if(userName.equals("xiaohu")){
            User user=new User();
            user.setId(100);
            user.setUserName("xiaohu");
            user.setPassword("123");
            user.setStatus("1");
            return user;
        }else if(userName.equals("lili")){
            User user=new User();
            user.setId(101);
            user.setUserName("lili");
            user.setPassword("123");
            user.setStatus("1");
            return user;
        }
        return null;
    }

    private List<Role> findRoleByUserName(String userName){
        if(userName.equals("xiaohu")){
            Role role=new Role();
            role.setId(100);
            role.setName("user operation");
            role.setMemo("操作用户");
            return Arrays.asList(role);
        }
        return null;
    }

    private List<Permission> findPermissionByUserName(String userName){
        if(userName.equals("xiaohu")){
            Permission permission=new Permission();
            permission.setId(100);
            permission.setName("user:add");
            permission.setUrl("user/add");
            return Arrays.asList(permission);
        }
        return null;
    }
}
